<?php
namespace App\Models\Trade\Supply;

use App\Models\Model;

class SupplyCountVisitModel extends Model
{

    const TABLENAME = 'tw_trade_supply_count_visit';

    /**
     * 查询一条数据
     *
     * @param int $supplyId            
     * @param array $fields            
     * @return \Illuminate\Support\Collection
     */
    public static function findBySupplyId($supplyId, array $fields = [])
    {
        $query = self::dbTable();
        if (count($fields)) {
            $query->select($fields);
        }
        
        return $query->where('supply_id', $supplyId)->get();
    }

    /**
     * 统计自增
     *
     * @param string $field            
     * @param int $supplyId            
     */
    protected static function inc($field, $supplyId)
    {
        if (! $supplyId) {
            return false;
        }
        $query = self::dbTable();
        
        $number = self::findBySupplyId($supplyId)->count();
        if ($number) {
            return $query->where('supply_id', $supplyId)->increment($field);
        } else {
            return $query->insert([
                'supply_id' => $supplyId,
                $field => 1
            ]);
        }
    }

    /**
     * 统计自减
     *
     * @param string $field            
     * @param int $supplyId            
     */
    protected static function dec($field, $supplyId)
    {
        if (! $supplyId) {
            return false;
        }
        $query = self::dbTable();
        
        $item = self::findBySupplyId($supplyId)->shift();
        if ($item && $item[$field] > 0) {
            return $query->where('supply_id', $supplyId)->decrement($field);
        }
        return false;
    }

    /**
     * 统计自减（批量修改）
     *
     * @param string $field            
     * @param int $supplyIds            
     * @param int $userId            
     */
    protected static function decBySupplyIds($field, $supplyIds, $userId)
    {
        if (! $supplyIds || ! count($supplyIds) || ! $userId) {
            return false;
        }
        $where[] = ['dc.user_id', $userId];
        $where[] = ["dcv.$field", '>', 0];
        
        return self::dbTable('dcv')->where($where)
            ->join(SupplyCollectionModel::TABLENAME . ' as dc', 'dc.supply_id', '=', 'dcv.supply_id')
            ->whereIn('dcv.supply_id', $supplyIds)
            ->decrement("dcv.$field");
    }

    /**
     * pv +1
     *
     * @param int $supplyId            
     */
    public static function incPv($supplyId)
    {
        return self::inc('pv', $supplyId);
    }

    /**
     * uv +1
     *
     * @param int $supplyId            
     */
    public static function incUv($supplyId)
    {
        return self::inc('uv', $supplyId);
    }

    /**
     * collection +1
     *
     * @param int $supplyId            
     */
    public static function incCollection($supplyId)
    {
        return self::inc('collection', $supplyId);
    }

    /**
     * collection -1
     *
     * @param int $supplyId            
     */
    public static function decCollection($supplyId)
    {
        return self::dec('collection', $supplyId);
    }

    /**
     * collection -1
     *
     * @param int $supplyIds            
     * @param int $userId            
     */
    public static function decCollectionBySupplyIds($supplyIds, $userId)
    {
        return self::decBySupplyIds('collection', $supplyIds, $userId);
    }

    /**
     * report +1
     *
     * @param int $supplyId            
     */
    public static function incReport($supplyId)
    {
        return self::inc('report', $supplyId);
    }

    /**
     * dial +1
     *
     * @param int $supplyId            
     */
    public static function incDial($supplyId)
    {
        return self::inc('dial', $supplyId);
    }
}
